package org.leetcode.easy.leetcode202;

import java.util.HashSet;
import java.util.Set;

public class Solution
{
    public boolean isHappy(int n) {

          Set<Integer> record = new HashSet<>();

          while (n!=1 && !record.contains(n))
          {
              record.add(n);
              n = getNextNumber(n);
          }

          return n==1;
    }

    /**
     * 计算数字上每个位数上之和
     * @param n
     * @return
     */
    private int getNextNumber(int n)
    {
        int res =0;
        while (n>0)
        {
            int temp = n % 10;
            res=res+temp*temp;
            n=n/10;
        }

        return res;
    }

    public static void main(String[] args)
    {
        Solution solution = new Solution();
        int number = 123;

        boolean isHappy = solution.isHappy(number);

        System.out.println(isHappy);
    }
}
